package com.simtop.controller;

import com.simtop.common.ServerResponse;
import com.simtop.pojo.WebMessage;
import com.simtop.service.WebMessageService;
import com.simtop.util.FileUploadAndDownload;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author lvyongwei
 */
@CrossOrigin
@RestController
@RequestMapping("/message")
@Api(tags = "网页详情模块")
public class WebMessageController {

    @Autowired
    private WebMessageService webMessageService;

    @Autowired
    private FileUploadAndDownload fileUploadAndDownload;

    /**
     * 获取所有网页内容信息
     * @return
     */
    @GetMapping("/allContent")
    @ApiOperation("获取所有网页内容信息")
    public ServerResponse<Object> getAllMessage(){
        try{
            List<WebMessage> allMessage = webMessageService.getAllMessage();
            return ServerResponse.createBySuccess("获取信息成功",allMessage);
        }catch (Exception e){
            e.printStackTrace();
            return ServerResponse.createByErrorMsg("获取信息失败");
        }
    }

    /**
     * 修改内容信息
     * @return
     */
    @PostMapping("/updateContent")
    @ApiOperation("修改内容信息")
    public ServerResponse<Object> updateAllMessage(@ApiParam("网页信息实体") WebMessage webMessage, @ApiParam("文件内容") MultipartFile file){
        /**
         * 判断是否是文件上传
         */
        if(file != null){
            String upload = fileUploadAndDownload.upload(file);
            webMessage.setFilePath(upload);
        }
        try{
            webMessageService.updateMessage(webMessage);
            return ServerResponse.createBySuccessMsg("修改信息成功");
        }catch(Exception e){
            return ServerResponse.createByErrorMsg("修改信息失败");
        }
    }
}
